package com.internetCafes.spms.web.certificate.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.internetCafes.spms.common.CommonConstants;
import com.internetCafes.spms.core.exception.RRException;
import com.internetCafes.spms.core.exception.RRExceptionCodeEnum;
import com.internetCafes.spms.core.generic.GenericDao;
import com.internetCafes.spms.core.generic.GenericServiceImpl;
import com.internetCafes.spms.core.model.pageModel.DataGrid;
import com.internetCafes.spms.web.certificate.dao.EmployeeInfoMapper;
import com.internetCafes.spms.web.certificate.entity.EmployeeInfo;
import com.internetCafes.spms.web.certificate.model.employee.podo.EmployeeDo;
import com.internetCafes.spms.web.certificate.model.employee.podo.EmployeePo;
import com.internetCafes.spms.web.certificate.model.podo.EmployeeInfoDo;
import com.internetCafes.spms.web.certificate.model.podo.EmployeeInfoPo;
import com.internetCafes.spms.web.certificate.service.EmployeeInfoService;
import com.internetCafes.spms.web.certificate.vo.EmployeeInfoVo;
import com.internetCafes.spms.web.certificate.vo.EntryQuitInfo;
import com.sms.common.util.hutool.core.collection.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 员工信息表 EmployeeInfoService 实现类
 *
 * @author Sunny
 * @email rekeeper2011@hotmail.com
 * @date 2019-10-31 18:10:59
 */
@Service("employeeInfoService")
public class EmployeeInfoServiceImpl extends GenericServiceImpl<EmployeeInfo, Long> implements EmployeeInfoService {

    @Autowired
    private EmployeeInfoMapper employeeInfoMapper;

    @Override
    public EmployeeInfo findByIdNotNull(Long id) {
        EmployeeInfo employeeInfo = findById(id);
        if (ObjectUtil.isNull(employeeInfo)) {
            throw new RRException(RRExceptionCodeEnum.PRJ_MEMBER_NOT_EXIST);
        }
        return employeeInfo;
    }

    /**
     * 获取员工列表
     *
     * @param employeeDo 请求参数
     * @return 数据列表
     */
    @Override
    public List<EmployeePo> list(EmployeeDo employeeDo) {
        return employeeInfoMapper.list(employeeDo);
    }

    @Override
    public List<EmployeeInfoPo> listPoNoTenantId(EmployeeInfoDo employeeInfoDo) {
        return employeeInfoMapper.listPoNoTenantId(employeeInfoDo);
    }

    /**
     * 获取人员列表，并进行去重操作，获取人员
     *
     * @return 人员列表
     * @author Zheng
     */
    @Override
    public List<EmployeeInfo> queryRepeat() {
        return employeeInfoMapper.queryRepeat();
    }

    /**
     * 为对应企业id获取employeeFlag为1，deptId为null的人员信息进行更新部门id，
     *
     * @param tenantId 企业id
     * @param deptId   部门id
     * @author Zheng
     */
    @Override
    public void updateNonDeptIdEmployees(Long tenantId, Long deptId) {
        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", tenantId);
        params.put("deptId", deptId);
        employeeInfoMapper.updateNonDeptIdEmployees(params);
    }

    /**
     * 获取employeeFlag为1，deptId为null 或者 0 的数据
     *
     * @return 员工信息列表
     * @author Zheng
     */
    @Override
    public List<EmployeeInfo> queryEmployeeNonDeptId() {
        return employeeInfoMapper.queryEmployeeNonDeptId();
    }

    /**
     * 将企业用户中所有employeeFlag为1，type为0的数据，将type置为23
     *
     * @author Zheng
     */
    @Override
    public void repairEmployeeTypeError() {
        employeeInfoMapper.repairEmployeeTypeError();
    }

    /**
     * 员工employee_flag错误信息修复
     *
     * @author Zheng
     */
    @Override
    public void repairEmployeeFlagErrorInfo() {
        employeeInfoMapper.repairEmployeeFlagErrorInfo();
    }

    /**
     * 删除多余的员工信息
     *
     * @param idCard   身份证号
     * @param id       不删除的id
     * @param tenantId 企业id
     * @author Zheng
     */
    @Override
    public void removalRepeatEmployee(String idCard, Long tenantId, Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("idCard", idCard);
        params.put("id", id);
        params.put("tenantId", tenantId);
        employeeInfoMapper.removalRepeatEmployee(params);
    }

    /**
     * 获取重复人员信息列表 id 身份证
     *
     * @return 人员信息
     */
    @Override
    public List<EmployeeInfo> queryListForRepeat() {
        return employeeInfoMapper.queryListForRepeat();
    }

    @Override
    public List<EmployeeInfo> query(Map<String, Object> params) {
        return employeeInfoMapper.selectByParams(params);
    }

    @Override
    public EmployeeInfo findById(Long id) {
        return employeeInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public EntryQuitInfo countByMonth(EmployeeInfo employeeInfo) {
        return employeeInfoMapper.countByMonth(employeeInfo);
    }

    @Override
    public List<EmployeeInfo> queryByIds(List<Long> ids) {
        Map<String, Object> params = new HashMap<>();
        params.put("ids", ids);
        return employeeInfoMapper.selectByParams(params);
    }

    @Override
    public List<EmployeeInfo> page(Map<String, Object> params, int page, int rows) {
        params.put("page", page);
        params.put("rows", rows);
        return employeeInfoMapper.selectByParams(params);
    }

    @Override
    public long count(Map<String, Object> params) {
        return employeeInfoMapper.countByParams(params);
    }

    @Override
    public EmployeeInfo findFirst(Map<String, Object> params) {
        List<EmployeeInfo> employeeList = employeeInfoMapper.selectByParams(params);
        EmployeeInfo employeeInfo = null;
        if (CollectionUtil.isNotEmpty(employeeList)) {
            employeeInfo = CollectionUtil.getFirst(employeeList);
        }
        return employeeInfo;
    }

    @Override
    public int save(EmployeeInfo employeeInfo) {
        return employeeInfoMapper.insert(employeeInfo);
    }

    @Override
    public int saveBatch(List<EmployeeInfo> employeeInfoList) {
        return employeeInfoMapper.insertBatch(employeeInfoList);
    }

    @Override
    public int saveSelective(EmployeeInfo employeeInfo) {
        return employeeInfoMapper.insertSelective(employeeInfo);
    }

    @Override
    public int update(EmployeeInfo employeeInfo) {
        return employeeInfoMapper.updateByPrimaryKey(employeeInfo);
    }

    @Override
    public int updateEntire(EmployeeInfo employeeInfo) {
        return employeeInfoMapper.updateEntireByPrimaryKey(employeeInfo);
    }

    @Override
    public int updateSelective(EmployeeInfo employeeInfo) {
        return employeeInfoMapper.updateByPrimaryKeySelective(employeeInfo);
    }

    @Override
    public int delete(Long id) {
        return employeeInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteBatch(List<Long> ids) {
        return employeeInfoMapper.deleteBatchByPrimaryKey(ids);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        if (ids != null && ids.length > 0) {
            for (Long id : ids) {
                EmployeeInfo e = findById(id);
                e.setStatus(CommonConstants.CommonStatus.DISABLE);
                update(e);
            }
        }
    }

    @Override
    public GenericDao<EmployeeInfo, Long> getDao() {
        return employeeInfoMapper;
    }

    /**
     * DataGrid
     */
    @Override
    public DataGrid dataGrid(Map<String, Object> params, int pageNo, int pageSize) {

//        params.put("pageNo", (pageNo - 1 < 0 ? -1 : pageNo - 1) * pageSize);
//        params.put("pageSize", pageSize);
        if ((params.get("orderBy") != null && "gender".equals(params.get("orderBy"))) || (params.get("maxAge") != null && params.get("minAge") != null) || params.get("gender") != null) {
            List<EmployeeInfoVo> employeeInfoVoList = employeeInfoMapper.selectByParamsUnionDept(params);
            List<EmployeeInfoVo> resultList = new ArrayList<>(employeeInfoVoList);
            if (params.get("orderBy") != null && "gender".equals(params.get("orderBy"))) {
                resultList.sort((o1, o2) -> ("asc".equals(params.get("inOrder"))) ? (o1.getGender() - o2.getGender()) : (o2.getGender() - o1.getGender()));
            }
            if (params.get("maxAge") != null && params.get("minAge") != null) {
                Integer maxAge = (Integer) params.get("maxAge");
                Integer minAge = (Integer) params.get("minAge");
                for (EmployeeInfoVo employeeInfoVo : employeeInfoVoList) {
                    if (employeeInfoVo.getAge() == null || employeeInfoVo.getAge() < minAge || employeeInfoVo.getAge() > maxAge) {
                        resultList.remove(employeeInfoVo);
                    }
                }
            }
            if (params.get("gender") != null) {
                Integer gender = (Integer) params.get("gender");
                for (EmployeeInfoVo employeeInfoVo : employeeInfoVoList) {
                    if (employeeInfoVo.getGender() == null || !employeeInfoVo.getGender().equals(gender)) {
                        resultList.remove(employeeInfoVo);
                    }
                }
            }
            int size = resultList.size();
            List<EmployeeInfoVo> employeeInfoVos = resultList.subList((pageNo - 1) * pageSize, Math.min(pageNo * pageSize, resultList.size()));
            return new DataGrid(employeeInfoVos, size, pageSize, pageNo);
        }
        PageHelper.startPage(pageNo, pageSize);
//        List<EmployeeInfoVo> employeeInfoList = selectByParamsUnionDept(params);
        PageInfo<EmployeeInfoVo> pageInfo = new PageInfo<>(employeeInfoMapper.selectByParamsUnionDept(params));
//        long count = employeeInfoMapper.countByParamsUnionDept(params);
//        return new DataGrid(employeeInfoList, count, pageSize, pageNo);
        return new DataGrid(pageInfo.getList(), pageInfo.getTotal(), pageSize, pageNo);
    }


    @Override
    public List<EmployeeInfoVo> selectByParamsUnionDept(Map<String, Object> params) {
        return employeeInfoMapper.selectByParamsUnionDept(params);
    }

    @Override
    public List<EmployeeInfoVo> selectByParamsUnionTenant(Map<String, Object> params) {
        return employeeInfoMapper.selectByParamsUnionTenant(params);
    }


    @Override
    public List<EmployeeInfoVo> selectByJobParams(Map<String, Object> params) {
        return employeeInfoMapper.selectByJobParams(params);
    }

    @Override
    public List<EmployeeInfoVo> selectByParamsUnionCertificate(Map<String, Object> params) {
        return employeeInfoMapper.selectByParamsUnionCertificate(params);
    }

}
